package com.example.vote.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class SpringUtils {

    /**
     *
     * @return
     */
    public static String getOpenId (){
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
        String openId = request.getHeader("X-WX-OPENID");
        if (!StringUtils.hasLength(openId)){
            openId = request.getHeader("X-WX-FROM-OPENID");
        }
        return openId;
    }
}
